package org.bambrikii.examples.rutext.main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.bambrikii.examples.rutext.lexer.custom.SentenceLexer;
import org.bambrikii.examples.rutext.parser.DefaultParser;
import org.bambrikii.examples.rutext.parser.IParser;
import org.bambrikii.examples.rutext.parser.ParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sampullara.cli.Args;
import com.sampullara.cli.Argument;

public class SentenceParserMain {

	private static Logger logger = LoggerFactory.getLogger(SentenceParserMain.class);

	@Argument(value = "input", description = "The input file", required = true)
	private String inputFilename;

	@Argument(value = "output", description = "The output file", required = true)
	private String outputFilename;

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException {

		SentenceParserMain main = new SentenceParserMain();

		Args.usage(main);
		// List<String> extra =
		Args.parse(main, args);

		BufferedReader reader = new BufferedReader(new FileReader(new File(main.inputFilename)));
		StringBuilder sb = new StringBuilder();
		try {
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
				sb.append(System.getProperty("line.separator"));
				logger.debug("line " + line);
			}
			reader.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		IParser parser = new DefaultParser(sb.toString());
		SentenceLexer lexer = new SentenceLexer(parser);
		try {
			String result = lexer.lex();
			logger.debug("result: " + result);
			try {
				BufferedWriter writer = new BufferedWriter(new FileWriter(new File(main.outputFilename)));
				writer.write(result);
				writer.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		} catch (ParserException e) {
			throw new RuntimeException(e);
		}

	}
}
